package duck.dao.impl;

import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import log4j.BaseLog;

import org.hibernate.*;

import pojo.Actor;
import pojo.Actormodule;
import pojo.Module;
import pojo.Persion;

import common.SessionFactory;
import duck.dao.UserDao;
public class UserManagerImpl extends BaseLog  implements UserDao {

	public boolean checkExists(String name) throws Exception {
		
		return false;
	}

	public Persion login(String name, String password) throws Exception {
		Persion user=null;
		Session s=SessionFactory.getSession();
		try{
			user=(Persion)s.createQuery("from Persion where uname=? and upassword=? ").setString(0, name).setString(1, password).uniqueResult();
			if(!Hibernate.isInitialized(user.getActor()))
				Hibernate.initialize(user.getActor());
				Hibernate.initialize(user.getDepartment());
			}catch(Exception e){
			log.info(e);
			user=null;
		}
		
		finally{
			SessionFactory.closeSession();
		}
		
		return user;
	}
	

	public Persion load(Integer id, boolean lazy) throws Exception {
		Session s=SessionFactory.getSession();
		Persion p=null;
		try{
			p=(Persion)s.get(Persion.class, id);
			if(lazy==true)
				Hibernate.initialize(p.getActor());
		}catch(Exception e){
			log.error(e);
		}
		finally{
			SessionFactory.closeSession();
		}
		return p;
	}
	


	public Persion loginV2(String name, String password) throws Exception {
		Persion user=null;
		Session s=SessionFactory.getSession();
		try{
			user=(Persion)s.createQuery("from Persion where uname=? and upassword=? ").setString(0, name).setString(1, password).uniqueResult();
				if(user!=null){
					if(!Hibernate.isInitialized(user.getActor()))
						Hibernate.initialize(user.getActor());
						Hibernate.initialize(user.getDepartment());
				}
				
			}catch(Exception e){
			log.info(e);
			user=null;
		}
		
		finally{
			SessionFactory.closeSession();
		}
		
		return user;
	}


}
